package poi

import (
	"context"
	rentalpb "coolcar/rental/api/gen/v1"
	"hash/fnv"

	"google.golang.org/protobuf/proto"
)

var poi = []string{
	"白云山",
	"广州塔",
	"越秀公园",
	"陈家祠",
	"华南植物园",
	"宝墨园",
	"南沙湿地",
}

// Manager defines a poi manager.
type Manager struct {
}

// Resolve resolves the given location.
func (*Manager) Resolve(ctx context.Context, loc *rentalpb.Location) (string, error) {
	// 计算哈希值
	// 1.把loc转成byte数组
	b, err := proto.Marshal(loc)
	if err != nil {
		return "", err
	}

	// 2.写入
	h := fnv.New32()
	h.Write(b)

	// 3.取出
	hashNum := h.Sum32()

	// return poi[rand.Intn(len(poi))], nil
	// 因为hash出来的数很大,可能不在poi切片内,所以需要对其取模。
	return poi[int(hashNum)%len(poi)], nil
}
